//
//  HPUser.m
//  LockTest
//
//  Created by ZhangBo on 2018/12/2.
//  Copyright © 2018 ZhangBo. All rights reserved.
//

#import "HPUser.h"
#import "HPUserBuilder.h"


@interface HPUser ()
@property(nonatomic, assign) BOOL frozen;
@end

@implementation HPUser

//  模型自定义初始化器的实现
- (instancetype)initWithBuilder:(HPUserBuilder *)builder {
    if (self = [super init]) {
        self.userId = builder.userId;
        self.firstName = builder.firstName;
        self.lastName = builder.lastName;
        self.gender = builder.gender;
        self.dateOfBirth = builder.dateOfBirth;
        self.albums = [NSArray arrayWithArray:builder.albums];
    }
    return self;
}


- (instancetype)initWithDictionary:(NSDictionary *)dictionary {
    if (self = [super init]) {
        self.userId = dictionary[@"userId"];
        self.firstName = dictionary[@"firstName"];
        self.lastName = dictionary[@"lastName"];
        self.gender = dictionary[@"gender"];
        self.dateOfBirth = dictionary[@"dateOfBirth"];
    }
    return self;
}

//  userWithBlock: 方法的实现
+ (instancetype)userWithBlock:(void (^)(HPUserBuilder *))block {
    HPUserBuilder *builder = [[HPUserBuilder alloc] init];
    block(builder);
    return [builder build];
}


- (void)freeze {
    self.frozen = YES;
}

- (void)setUserId:(NSString *)userId {
    if (!self.frozen) {
        _userId = userId;
    }
}

- (void)setFirstName:(NSString *)firstName {
    if (!self.frozen) {
        _firstName = firstName;
    }
}

- (void)setLastName:(NSString *)lastName {
    if (!self.frozen) {
        _lastName = lastName;
    }
}

- (void)setGender:(NSString *)gender {
    if (!self.frozen) {
        _gender = gender;
    }
}

- (void)setDateOfBirth:(NSDate *)dateOfBirth {
    if (!self.frozen) {
        _dateOfBirth = dateOfBirth;
    }
}

- (void)setAlbums:(NSArray *)albums {
    if (!self.frozen) {
        _albums = albums;
    }
}

@end
